//
//  MeSourceDetailViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/10/8.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "MeSourceDetailViewController.h"
#import "MeSourceDetailViewCell.h"
#import "taskUserManagerService.h"
#import "dataSourceList.h"

@interface MeSourceDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView    *tableView;
@property (nonatomic,assign)NSInteger      isGTE;
@property (nonatomic,assign)NSInteger      page;
@property (nonatomic,strong)     NSMutableArray *dataArray;
@end

@implementation MeSourceDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = self.isGTE == 0 ? @"GTE明细" : @"GTK明细";
    [self loadData];
    [self.view addSubview:self.tableView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
}

- (id)initWithIsGTE:(NSInteger )isGTE {
    self = [super init];
    if (self) {
        self.isGTE = isGTE;
    }
    return self;
}

-(void)loadData{
    self.page = 1;
    [self loadlistData];
}

-(void)loadMoreData{
    self.page ++;
    [self loadlistData];
}

-(void)loadlistData{
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setField:[NSString stringWithFormat:@"%ld",self.page] value:@"page"];
    [dict setField:@"10" value:@"pageSize"];
    [dict setField:[NSString stringWithFormat:@"%ld",self.isGTE] value:@"sourceType"];
    [taskUserManagerService getDetailRecord:dict success:^(dataSourceList *response, BOOL success) {
        if (success) {
            if (self.page == 1) {
                self.dataArray = response.data;
                [self.tableView.mj_header endRefreshing];
            }else{
                NSArray* array = response.data;
                [self.dataArray addObjectsFromArray:array];
                [self.tableView.mj_footer endRefreshing];
            }
            [self.tableView reloadData];
            return ;
        }
        [DKProgressHUD showErrorWithStatus:response.statusDesc];
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setBlackStatus:YES];
    if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

#pragma mark - init
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 114, KScreenWidth, KScreenHeight-114) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.scrollEnabled = YES;
        _tableView.sectionHeaderHeight = 0.00001;
        _tableView.sectionFooterHeight = 0.00001;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeSourceDetailViewCell *cell = [MeSourceDetailViewCell cellWithTableView:tableView];
    dataSourceInfo *model = [dataSourceInfo mj_objectWithKeyValues:[self.dataArray objectAtIndex:indexPath.row]];
    cell.lbcategory.text = model.category;
    cell.lbdate.text = model.date;
    cell.lbprofit.text = model.profit;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
